package cn.java.lambda.test3;

import java.util.function.Consumer;

/**
 * 测试Consumer接口
 * @author 攀博课堂(www.pbteach.com)
 * @version 1.0
 **/
public class TestConsumer {
    //一个整型数组
    static int[] datas = {1,2,3,4,5,6,7};
    //静态变量
    static int sum = 0;
    public static void main(String[] args) {
        //定义一个Consumer接口对象
        Consumer<Integer> fun1 = n -> System.out.println(n);
        //判断奇偶
        Consumer<Integer> fun2 = n ->{
            if(n % 2 == 0){
                System.out.println(n + "是偶数");
            }else{
                System.out.println(n + "是奇数");
            }
        };
        //求累加和
        Consumer<Integer> fun3 = n -> sum+=n;
       //调用consumer
        consumer(fun1,datas);
        System.out.println("===========判断奇偶=========");
        consumer(fun2,datas);
        System.out.println("===========求累加和=========");
        consumer(fun3,datas);
        System.out.println("sum="+sum);
    }

    //消费方法
    public static void consumer(Consumer<Integer> fun,int[] datas){
        for (int i = 0; i < datas.length; i++) {
             //调用accept方法进行消费,一次消费一个数据
            fun.accept(datas[i]);
        }
    }
}
